package com.yuntongxun.ecdemo.voicechat.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.voicechat.bean.Gift;
import com.yuntongxun.ecdemo.voicechat.listeners.IRecyclerViewItemListener;

import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/12/8.
 */

public class GiftsReceivedAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<Gift> notes;
    private IRecyclerViewItemListener mListener;

    public static final String TYPES="types";

    public static final int RECEIVED=0;//接收
    public static final int SENT=01;//赠送
    private int type=RECEIVED;

    public GiftsReceivedAdapter(Context context, List<Gift> noteList, IRecyclerViewItemListener myItemClickListener) {
        mContext = context;
        notes = noteList;
        mListener = myItemClickListener;
    }

    public void setGiftType(int type){
        this.type=type;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = View.inflate(parent.getContext(), R.layout.adapter_receive_gift_item, null);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        GiftsReceivedAdapter.ViewHolder viewHolder = (GiftsReceivedAdapter.ViewHolder) holder;
        viewHolder.nameTv.setText(notes.get(position).getName());
//        viewHolder.priceTv.setText("x" + notes.get(position).getAmount());

        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d = format.format(notes.get(position).getCreateTime());
        viewHolder.timeTv.setText(d);
        Picasso.with(mContext)
                .load(notes.get(position).getUrl())
                .placeholder(R.drawable.ic_register_logo)
                .tag(mContext)
                .into(viewHolder.giftImg);

        //我收到的礼物列表状态应该就只有1跟2，1就是还没有兑换的，2就是已经兑换过了
        if (type==RECEIVED){
            viewHolder.whoTv.setText(notes.get(position).getHandselName());
            if (notes.get(position).getStatus().equals("1")){
                //可以兑换过的
                viewHolder.exchangedBtn.setVisibility(View.VISIBLE);
                viewHolder.operateTv.setVisibility(View.GONE);
            }else if(notes.get(position).getStatus().equals("3")){
                //转赠过来的
                viewHolder.exchangedBtn.setVisibility(View.VISIBLE);
                viewHolder.operateTv.setVisibility(View.GONE);
            }else{
                //已经兑换过的
                viewHolder.exchangedBtn.setVisibility(View.GONE);
                viewHolder.operateTv.setVisibility(View.VISIBLE);
                viewHolder.operateTv.setText("Exchanged");
            }


        }else{
            viewHolder.titleTv.setText("To:");
            viewHolder.whoTv.setText(notes.get(position).getSendTo());
            viewHolder.operateTv.setVisibility(View.GONE);
            viewHolder.exchangedBtn.setVisibility(View.GONE);
        }

    }

    @Override
    public int getItemCount() {
        if (notes != null) {
            return notes.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {


        @InjectView(R.id.gift_name)
        TextView nameTv;
        @InjectView(R.id.gift_item_img)
        ImageView giftImg;
        @InjectView(R.id.gift_item_price)
        TextView priceTv;
        @InjectView(R.id.gift_item_from)
        TextView whoTv;
        @InjectView(R.id.gift_item_from_title)
        TextView titleTv;
        @InjectView(R.id.gift_item_time)
        TextView timeTv;
        @InjectView(R.id.gift_operate_tv)
        TextView operateTv;
        @InjectView(R.id.gift_exchanged_btn)
        Button exchangedBtn;

        @OnClick(R.id.gift_exchanged_btn)
        public void onExchangedClick(){
            if (mListener!=null){
                mListener.onItemClick(getPosition());
            }
        }

        public ViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }

    }
}
